package com.heroCW.bodhiAttic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heroCW.bodhiAttic.common.BaseContext;
import com.heroCW.bodhiAttic.entity.ShoppingCart;
import com.heroCW.bodhiAttic.mapper.ShoppingCartMapper;
import com.heroCW.bodhiAttic.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public ShoppingCart getOneShoppingCart(ShoppingCart shoppingCart) {
        // 设置用户Id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        // 添加到购物车的是菜品
        queryWrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        // 添加到购物车的是套餐
        queryWrapper.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        return this.getOne(queryWrapper);
    }

    @Override
    @Transactional
    public Boolean clean() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        return this.remove(queryWrapper);
    }

    @Override
    public List<ShoppingCart> getList() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        return this.list(queryWrapper);
    }
}
